package com.hua.store.web.servlet;

import com.hua.store.domin.Product;
import com.hua.store.service.ProductService;
import com.hua.store.service.impl.ProductServiceImpl;
import com.hua.store.utils.UUIDUtils;
import com.hua.store.utils.UploadUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@WebServlet(name = "AddProductServlet", urlPatterns = "/AddProductServlet")
public class AddProductServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String> map = new HashMap<String, String>();
        //新建一个product用来携带数据
        Product product = new Product();
        //三行代码
        try {
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List<FileItem> list = upload.parseRequest(req);
            //遍历集合
            for (FileItem item : list) {
                if (item.isFormField()) {
                    map.put(item.getFieldName(), item.getString("utf-8"));
                } else {
                    //获取文件名
                    String fileName = item.getName();
                    //生成新的文件名
                    String newFileName = UploadUtils.getUUIDName(fileName);
                    //获取图片文件存放的tomcat地址
                    String realPath = getServletContext().getRealPath("products/3");
                    //根据文件名生成8位的目录地址
                    String eightDir = UploadUtils.getDir(newFileName);

                    // 拼接并创建新的目录地址  *判断目录是否存在*
                    File finalDir = new File(realPath + eightDir);


                    System.out.println("finalDir" + finalDir);


                    if (!finalDir.exists()) {
                        finalDir.mkdirs();
                    }
                    //创建文件 *判断文件是否存在* 在新目录下放置新的文件名
                    File finalFile = new File(finalDir, newFileName);
                    if (!finalFile.exists()) {
                        finalFile.createNewFile();
                    }

                    //创建输入输出流
                    OutputStream os = new FileOutputStream(finalFile);
                    InputStream is = item.getInputStream();
                    //copy
                    IOUtils.copy(is, os);
                    IOUtils.closeQuietly(is);
                    IOUtils.closeQuietly(os);

                    product.setPimage("products/3" + eightDir + "/" + newFileName);
                }


            }


            BeanUtils.populate(product, map);

            product.setPid(UUIDUtils.getId());
            product.setPdate(new Date());
            product.setPflag(0);

            ProductService service = new ProductServiceImpl();

            service.saveProduct(product);
        } catch (Exception e) {
            e.printStackTrace();
        }

//        req.getRequestDispatcher("/AdminProductServlet?method=findAllProductWithPage").forward(req, resp);
//        resp.sendRedirect("/AdminProductServlet?method=findAllProductWithPage");
//        resp.sendRedirect(req.getContextPath() + "/admin/product/list.jsp");
        resp.sendRedirect("/AdminProductServlet?method=findAllProductWithPage&num=1");
    }
}
